package me.maiz.tool.codegen.controller;


import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import me.maiz.tool.codegen.domain.OrderDO;
import me.maiz.tool.codegen.service.OrderService;
import me.maiz.tool.codegen.common.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-09-29 10:40:21 | Aron</small>
 */
@Controller
@RequestMapping("/codegen/order")
public class OrderController extends AdminBaseController {
	@Autowired
	private OrderService orderService;
	
	@GetMapping()
	String Order(){
	    return "order/order";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public Result<Page<OrderDO>> list(OrderDO orderDTO){
        Wrapper<OrderDO> wrapper = new EntityWrapper<OrderDO>().orderBy("orderId", false);
        Page<OrderDO> page = orderService.selectPage(getPage(OrderDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	String add(){
	    return "order/add";
	}

	@GetMapping("/edit/{orderId}")
	String edit(@PathVariable("orderId") Integer orderId,Model model){
		OrderDO order = orderService.selectById(orderId);
		model.addAttribute("order", order);
	    return "order/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public Result<String> save( OrderDO order){
		orderService.insert(order);
        return Result.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public Result<String>  update( OrderDO order){
		boolean update = orderService.updateById(order);
		return update ? Result.ok() : Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public Result<String>  remove( Integer orderId){
		orderService.deleteById(orderId);
        return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public Result<String>  remove(@RequestParam("ids[]") Integer[] orderIds){
		orderService.deleteBatchIds(Arrays.asList(orderIds));
		return Result.ok();
	}
	
}
